package com.ray.service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ray.bean.MaterialPositionBean;
import com.ray.mapper.MaterialPositionMapper;

import net.sf.json.JSONArray;

/**
 * 物料仓位service
 * @author libin
 *
 */
@Service
public class MaterialPositionService {
	
	@Autowired
	MaterialPositionMapper materialPositionMapper;
	
	/**
	 * 新增仓位物料对应表
	 */
	public void addMaterialPositionList(Map<String,Object> map) {
		List<MaterialPositionBean> materialPositionList =  (List<MaterialPositionBean>) map.get("materialPositionList");
		List<MaterialPositionBean> positionList = materialPositionMapper.getMaterialPositionList(map);
		materialPositionList.addAll(positionList);
		List<MaterialPositionBean> addMaterialPositionList = materialPositionList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getMaterialCode() + ";" + o.getPosition()))), ArrayList::new));
		map.put("materialPositionList", addMaterialPositionList);
		if(addMaterialPositionList.size()>0) {
			int row = materialPositionMapper.addMaterialPositionList(map);
			Assert.isTrue(row>0, "新增仓位信息失败");
		}
	}
	
	public void addMaterialPosition(MaterialPositionBean materialPositionBean) {
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("loginCompany", materialPositionBean.getCompany());
		paramMap.put("materialCode", materialPositionBean.getMaterialCode());
		List<MaterialPositionBean> positionList = materialPositionMapper.getMaterialPositionList(paramMap);
		boolean isAdd = true;
		for(MaterialPositionBean position:positionList) {
			if(position.getMaterialCode().equals(materialPositionBean.getMaterialCode())
					&& position.getPosition().equals(materialPositionBean.getPosition())) {
				isAdd = false;
			}
		}
		if(isAdd) {
			int row = materialPositionMapper.addMaterialPosition(materialPositionBean);
			Assert.isTrue(row>0, "新增仓位信息失败");
		}
	}
	
	public void updateMaterialPositionList(Map<String,Object> map) {
		//修改 = 删除+新增
		Object materialPositionObj = map.get("materialPositionData");
		Assert.notNull(materialPositionObj, "物料仓位信息为空");
		List<MaterialPositionBean> materialPositionList =  (List<MaterialPositionBean>) JSONArray.toCollection(JSONArray.fromObject(materialPositionObj.toString()), MaterialPositionBean.class);
		//删除
		this.deleteMaterialPositionList(materialPositionList.get(0));
		//新增
		if(materialPositionList !=null && materialPositionList.size()>0) {
			for(MaterialPositionBean materialPositionBean:materialPositionList) {
				materialPositionBean.setCompany(map.get("loginCompany").toString());
			}
			map.put("materialPositionList", materialPositionList);
			int row = materialPositionMapper.addMaterialPositionList(map);
			Assert.isTrue(row>0, "修改仓位失败");
		}
	}

	private void deleteMaterialPositionList(MaterialPositionBean materialPosition) {
		materialPositionMapper.deleteMaterialPositionList(materialPosition);
	}
}
